package richbar.com.richbar.activity.me;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.common.base.BaseActivity;
import com.common.util.LogUtil;
import com.common.util.StringUtil;
import com.common.util.ToastUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;
import com.smarttop.library.bean.City;
import com.smarttop.library.bean.County;
import com.smarttop.library.bean.Province;
import com.smarttop.library.bean.Street;
import com.smarttop.library.widget.AddressSelector;
import com.smarttop.library.widget.BottomDialog;
import com.smarttop.library.widget.OnAddressSelectedListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import richbar.com.richbar.R;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.base.S;
import richbar.com.richbar.bean.AddAddressResult;
import richbar.com.richbar.bean.AddressResult;
import richbar.com.richbar.help.TitleHelp;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;

/**
 * Created by QiYang on 2018/12/14.
 */

public class EditorAddressAc extends BaseActivity implements OnAddressSelectedListener, AddressSelector.OnDialogCloseListener{
    @BindView(R.id.name)
    EditText name_tv;
    @BindView(R.id.tel)
    EditText tel_tv;
    @BindView(R.id.area)
    TextView area_tv;
    @BindView(R.id.address)
    EditText address_tv;
    private BottomDialog dialog;
    private Province province;
    private City city;
    private County county;
    private Street street;
    private LoadingDialog loadingDialog;
    AddressResult.AddressBean bean;

    @Override
    public int initLayout() {
        return R.layout.ac_editor_address;
    }

    @Override
    public void initTitle() {
        new TitleHelp(this).setTitle("编辑地址").setRightTv("完成", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }


    @Override
    public void initView() {
        ButterKnife.bind(this);
        dialog = new BottomDialog(this);
        loadingDialog = new LoadingDialog(this,0);
    }

    @Override
    public void initData() {
        bean = (AddressResult.AddressBean) getBundle().getSerializable("bean");
        name_tv.setText(bean.getName());
        tel_tv.setText(bean.getMobile());
        String area = bean.getFulladdress().substring(bean.getFulladdress().length() - bean.getFulladdress().length(),bean.getFulladdress().length());
        area_tv.setText(area);
        address_tv.setText(bean.getAddress());
    }


    private void submit() {
        String name = StringUtil.getStr(name_tv);
        String tel = StringUtil.getStr(tel_tv);
        String area = StringUtil.getStr(area_tv);
        String address = StringUtil.getStr(address_tv);
        if (StringUtil.isEmpty(name,tel,area,address)){
            ToastUtil.showToast(this,getString(R.string.plz_all));
            return;
        }
        loadingDialog.show();
        String provinceId = bean.getProvince();
        String cityId = bean.getCity();
        String countyId = bean.getArea();
        String streetId = bean.getStreet();
        if (null != province){
            provinceId = province.id + "";
            cityId = province.id + "";
            countyId = county.id + "";
            streetId = street.id + "";
        }
        NetWorkUtil.editRess(bean.getId(),BaseApplication.uid, name, tel, address, provinceId, cityId, countyId,streetId, Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                /*final AddAddressResult result = MyGson.getInstance().fromJson(data, AddAddressResult.class);
                if (checkIsOk(result)){
                    if (Integer.valueOf(result.getData().getId()) > 0){
                        ToastUtil.showToast(EditorAddressAc.this,getString(R.string.commit_success));
                        loadingDialog.dismiss();
                        Intent intent = new Intent();
                        setResult(RESULT_OK,intent);
                        finish();
                    }
                }*/
            }
        });
    }

    @OnClick(R.id.area)
    public void onViewClicked() {
        dialog.setOnAddressSelectedListener(this);
        dialog.setDialogDismisListener(this);
        dialog.show();
    }

    @Override
    public void onAddressSelected(Province province, City city, County county, Street street) {
        this.province = province;
        this.city = city;
        this.county = county;
        this.street = street;
        area_tv.setText((null == province ? "":province.name) +
                (null == city ? "" : city.name) +
                (null == county ? "" : county.name) +
                (null == street ? "":street.name));
        if (dialog != null){
            dialog.dismiss();
        }
    }

    @Override
    public void dialogclose() {
        if (dialog != null){
            dialog.dismiss();
        }
    }
}
